package com.bcmusic.file.controller;
import com.bcmusic.file.FastDFSFile;
import com.bcmusic.util.FastDFSClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件传输
 */
@RestController
@RequestMapping("file")
public class FileController {
    /**
     * 返回 图片的全路径
     *
     * @param file 页面的文件对象
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        if(file==null)
        {
            return null;
        }
        try {
            FastDFSFile fastdfsfile = new FastDFSFile(
                    file.getOriginalFilename(),//原来的文件名  1234.jpg
                    file.getBytes(),//文件本身的字节数组
                    StringUtils.getFilenameExtension(file.getOriginalFilename())
            );
            String[] upload = FastDFSClient.upload(fastdfsfile);

            return FastDFSClient.getTrackerUrl()+"/"+upload[0]+"/"+upload[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 搞不定这个bug只能屈服呜呜呜
     * @param opernFile
     * @return
     */
    @PostMapping("/uploadOpernFile")
    public String uploadOpernFile(MultipartFile opernFile) {
        if(opernFile==null)
        {
            return null;
        }
        try {
            FastDFSFile fastdfsfile = new FastDFSFile(
                    opernFile.getOriginalFilename(),//原来的文件名  1234.jpg
                    opernFile.getBytes(),//文件本身的字节数组
                    StringUtils.getFilenameExtension(opernFile.getOriginalFilename())
            );
            String[] upload = FastDFSClient.upload(fastdfsfile);

            return FastDFSClient.getTrackerUrl()+"/"+upload[0]+"/"+upload[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/uploadLyricsFile")
    public String uploadLyricsFile(MultipartFile lyricsFile) {
        if(lyricsFile==null)
        {
            return null;
        }
        try {
            FastDFSFile fastdfsfile = new FastDFSFile(
                    lyricsFile.getOriginalFilename(),//原来的文件名  1234.jpg
                    lyricsFile.getBytes(),//文件本身的字节数组
                    StringUtils.getFilenameExtension(lyricsFile.getOriginalFilename())
            );
            String[] upload = FastDFSClient.upload(fastdfsfile);

            return FastDFSClient.getTrackerUrl()+"/"+upload[0]+"/"+upload[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/uploadMusicFile")
    public String uploadMusicFile(MultipartFile musicFile) {
        if(musicFile==null)
        {
            return null;
        }
        try {
            FastDFSFile fastdfsfile = new FastDFSFile(
                    musicFile.getOriginalFilename(),//原来的文件名  1234.jpg
                    musicFile.getBytes(),//文件本身的字节数组
                    StringUtils.getFilenameExtension(musicFile.getOriginalFilename())
            );
            String[] upload = FastDFSClient.upload(fastdfsfile);

            return FastDFSClient.getTrackerUrl()+"/"+upload[0]+"/"+upload[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
